<template>
  <view class="viewport">
    <!-- 导航栏 -->
    <NavBar title="个人设置" :isBack="true"></NavBar>
    <!-- 头像 -->
    <view @click="onAvatarChange" class="avatar">
      <view class="avatar-content">
        <image class="image" :src="store.userInfo.avatar" mode="aspectFill" />
        <text class="text">点击修改头像</text>
      </view>
    </view>
    <!-- 表单 -->
    <view class="form">
      <!-- 表单内容 -->
      <view class="form-content">
        <view class="form-item">
          <text class="label">账号</text>
          <text class="account">{{ store.userInfo.username }}</text>
        </view>
        <view class="form-item">
          <text class="label">昵称</text>
          <input v-model="nickname" class="input" type="text" placeholder="请填写昵称" />
        </view>
      </view>
      <!-- 提交按钮 -->
      <button @click="submit" class="form-button">保 存</button>
    </view>
  </view>
</template>

<script setup>
import { UserStore } from '@/stores/user.js'
import { settingAvatar, settingNickname } from '@/api/my.js'
import { reactive, toRefs } from 'vue'
import config from '@/request/config.js'
const store = UserStore()
const state = reactive({
  nickname: "",
  avatar: ""
})

// 修改头像
const onAvatarChange = () => {
  // 调用拍照/选择图片
  uni.chooseImage({
    // 文件个数
    count: 1,
    success: (chooseImageRes) => {
      console.log("🚀 ~ handleUpload ~ chooseImageRes:", chooseImageRes)
      // 本地路径
      const tempFilePath = chooseImageRes.tempFilePaths[0]
      // 文件上传
      state.avatar = tempFilePath
      const token = uni.getStorageSync("token")
      uni.uploadFile({
        url: config.baseUrl + 'users/uploads', // [!code ++]
        name: 'image', // 后端数据字段名  // [!code ++]
        filePath: tempFilePath, // 新头像  // [!code ++]
        fileType: 'image',
        header: {
          'token': token,
          'Content-Type': 'multipart/form-data'
        },
        success: async (uploadFileRes) => {
          // 判断状态码是否上传成功
          // 提取头像
          console.log("🚀 ~ handleUpload ~ uploadFileRes:", uploadFileRes)
          const res = JSON.parse(uploadFileRes.data)
          // 当前页面更新头像
          store.userInfo.avatar = res.data.data
          // 更新 Store 头像
          await settingAvatar({ wxavatar: res.data.data })
          uni.showToast({ title: "头像更换成功" })
        },
      })
    },
  })
}

async function submit () {
  if (state.nickname) {
    await settingNickname({ name: state.nickname })
    store.userInfo.nickname = state.nickname
    uni.showToast({
      title: "修改成功"
    })
  } else {
    uni.showToast({
      title: "昵称不能为空",
      icon:"error"
    })
  }




}

const { nickname, avatar } = toRefs(state)

</script>



<style lang="scss">
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-size: auto 420rpx;
  background-repeat: no-repeat;
}

// 导航栏
.navbar {
  position: relative;

  .title {
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    color: #fff;
  }

  .back {
    position: absolute;
    height: 40px;
    width: 40px;
    left: 0;
    font-size: 20px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

// 头像
.avatar {
  text-align: center;
  width: 100%;
  height: 260rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .text {
    display: block;
    padding-top: 20rpx;
    line-height: 1;
    font-size: 26rpx;
  }
}

// 表单
.form {
  background-color: #f4f4f4;

  &-content {
    margin: 20rpx 20rpx 0;
    padding: 0 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  &-item {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx;
    background-color: #fff;
    font-size: 28rpx;
    border-bottom: 1rpx solid #ddd;

    &:last-child {
      border: none;
    }

    .label {
      width: 180rpx;
      color: #333;
    }

    .account {
      color: #666;
    }

    .input {
      flex: 1;
      display: block;
      height: 46rpx;
    }

    .radio {
      margin-right: 20rpx;
    }

    .picker {
      flex: 1;
    }

    .placeholder {
      color: #808080;
    }
  }

  &-button {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 30rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #27ba9b;
  }
}
</style>
